package cn.lau.pp.module.vr.controller.admin.device;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lau.pp.framework.common.pojo.PageParam;
import cn.lau.pp.framework.common.pojo.PageResult;
import cn.lau.pp.framework.common.pojo.CommonResult;
import cn.lau.pp.framework.common.util.object.BeanUtils;
import static cn.lau.pp.framework.common.pojo.CommonResult.success;

import cn.lau.pp.framework.excel.core.util.ExcelUtils;

import cn.lau.pp.framework.operatelog.core.annotations.OperateLog;
import static cn.lau.pp.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.lau.pp.module.vr.controller.admin.device.vo.*;
import cn.lau.pp.module.vr.dal.dataobject.device.DeviceDO;
import cn.lau.pp.module.vr.service.device.DeviceService;

@Tag(name = "管理后台 - vr设备")
@RestController
@RequestMapping("/vr/device")
@Validated
public class DeviceController {

    @Resource
    private DeviceService deviceService;

    @PostMapping("/create")
    @Operation(summary = "创建vr设备")
    @PreAuthorize("@ss.hasPermission('vr:device:create')")
    public CommonResult<Integer> createDevice(@Valid @RequestBody DeviceSaveReqVO createReqVO) {
        return success(deviceService.createDevice(createReqVO));
    }
    @PostMapping("/updateDeviceGame")
    @Operation(summary = "设备点播游戏")
    @PreAuthorize("@ss.hasPermission('vr:device:update')")
    public CommonResult<Boolean> updateDeviceGame(@Valid @RequestBody DeviceUpdateGameReqVO createReqVO) {
        deviceService.updateDeviceGame(createReqVO);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新vr设备")
    @PreAuthorize("@ss.hasPermission('vr:device:update')")
    public CommonResult<Boolean> updateDevice(@Valid @RequestBody DeviceSaveReqVO updateReqVO) {
        deviceService.updateDevice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除vr设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vr:device:delete')")
    public CommonResult<Boolean> deleteDevice(@RequestParam("id") Integer id) {
        deviceService.deleteDevice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得vr设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vr:device:query')")
    public CommonResult<DeviceRespVO> getDevice(@RequestParam("id") Integer id) {
        DeviceDO device = deviceService.getDevice(id);
        return success(BeanUtils.toBean(device, DeviceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得vr设备分页")
    @PreAuthorize("@ss.hasPermission('vr:device:query')")
    public CommonResult<PageResult<DeviceRespVO>> getDevicePage(@Valid DevicePageReqVO pageReqVO) {
        PageResult<DeviceDO> pageResult = deviceService.getDevicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出vr设备 Excel")
    @PreAuthorize("@ss.hasPermission('vr:device:export')")
    @OperateLog(type = EXPORT)
    public void exportDeviceExcel(@Valid DevicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceDO> list = deviceService.getDevicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "vr设备.xls", "数据", DeviceRespVO.class,
                        BeanUtils.toBean(list, DeviceRespVO.class));
    }

}